package boot.spring.service.impl;

import boot.spring.mapper.DeviceMapper;
import boot.spring.mapper.LeaveApplyMapper;
import boot.spring.po.DeviceApply;
import boot.spring.po.LeaveApply;
import boot.spring.service.DeviceService;
import boot.spring.service.LeaveService;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
@Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	IdentityService identityservice;
	@Autowired
	RuntimeService runtimeservice;
	@Autowired
	TaskService taskservice;

	@Autowired
	DeviceMapper deviceMapper;

	public ProcessInstance startWorkflow(DeviceApply deviceapply, String userid) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		deviceapply.setApply_time(sdf.format(new Date()));
		deviceapply.setUser_id(userid);
		deviceMapper.save(deviceapply);
		String businesskey=String.valueOf(deviceapply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
		identityservice.setAuthenticatedUserId(userid);
		//根据流程定义时候指定的id开启流程
		ProcessInstance instance=runtimeservice.startProcessInstanceByKey("review",businesskey);
		String instanceid=instance.getId();
		deviceapply.setProcess_instance_id(instanceid);
		deviceMapper.updateByPrimaryKey(deviceapply);
		return instance;
	}
}
